#include <QDebug>
#include<QLabel>
#include<QLineEdit>
#include "UserPersonal_ui_logic.h"
#include"globals.h"
UserPersonalLogic::UserPersonalLogic(UserPersonal *ui, QObject *parent)
    : ui(ui), QObject(parent), personInfoUi(nullptr), orderInfoUi(nullptr), addressInfoUi(nullptr)
{

    connect(ui->editButton, &QPushButton::clicked, this, &UserPersonalLogic::onPersonalInfoClicked);
    connect(ui->addressManageButton,
            &QPushButton::clicked,
            this,
            &UserPersonalLogic::onAddressManageClicked);
    getUserInfo();
    getAvatar();
   // fetchOrderList();
}
UserPersonalLogic::UserPersonalLogic(QObject *parent)
    : QObject(parent)
{
    // Constructor definition if you need it

}
void UserPersonalLogic::getUserInfo()
{
    qDebug() << "Fetching user info...";
    PptpMessage requestMessage;
    QJsonObject parameters;

    requestMessage.clear();
    requestMessage.setPath("user/getUserInfo");
    int userId = UserSession::getInstance().getUserId(); // 获取 userId
    parameters["userId"] = userId;
    requestMessage.setParameters(parameters);

    pptpclient.pptpConnect(IP, PORT, requestMessage, this, &UserPersonalLogic::onUserInfoResponse);
}

void UserPersonalLogic::onUserInfoResponse(PptpMessage responseMessage)
{
    QJsonObject responseParameters = responseMessage.getParameters();
    qDebug() << responseParameters;

    // Extract data and set in UI
    QString personName = responseParameters.value("personName").toString();
    QString sex = responseParameters.value("sex").toString();
    QString birthday = responseParameters.value("birthday").toString();
    QString email = responseParameters.value("email").toString();
    QString sign = responseParameters.value("sign").toString();

    // Update UI
    newPage4->nicknameLabel->setText("昵称: " + personName);  // Set text on QLabel
    newPage4->genderLabel->setText("性别: " + sex);           // Set text on QLabel
    newPage4->emailLabel->setText("邮箱: " + email);          // Set text on QLabel
    newPage4->signLabel->setText("个性签名: " + sign);        // Set text on QLabel
    // If you want to display sign, you can add a QLabel for that as well
}

void UserPersonalLogic::onPersonalInfoClicked()
{
    qDebug() << "个人信息按钮被点击";
    try
    {
        if (!personInfoUi)
        {
            personInfoUi = new PersonalInfoUI();
            personLogic = new PersonalInfoLogic(personInfoUi);
        }

        personInfoUi->show();
    }
    catch (...)
    {
    }
}

void UserPersonalLogic::onAddressManageClicked()
{
    qDebug() << "地址管理按钮被点击";
    try
    {
        if (!addressInfoUi)
        {
            addressInfoUi = new AddressUI();
            addressLogic = new AddressLogic(addressInfoUi);
        }

        addressInfoUi->show();
    }
    catch (...)
    {
    }
}


UserPersonalLogic::~UserPersonalLogic()
{
    delete personInfoUi;
    delete orderInfoUi;
    delete addressInfoUi;
}
void UserPersonalLogic::getAvatar()
{
    qDebug() << "Fetching user avatar...";

    PptpMessage requestMessage;
    QJsonObject parameters;
    parameters["userId"] = UserSession::getInstance().getUserId(); // 获取当前用户的ID
    requestMessage.clear();
    requestMessage.setParameters(parameters);
    requestMessage.setPath("user/getAvatar");
    pptpclient.pptpConnect(IP, PORT, requestMessage, this, &UserPersonalLogic::onGetAvatarResponse);
}

void UserPersonalLogic::onGetAvatarResponse(PptpMessage responseMessage)
{
    qDebug() << "Avatar fetched from server.";

    // 从响应消息中获取头像数据
    QHash<QString, QByteArray> files = responseMessage.getFiles();

    // 假设avatar文件存在于返回的数据中
    QByteArray responseData = files.value("avatar");

    if (responseData.isEmpty())
    {
        qDebug() << "No avatar data found in the response.";
        return;
    }

    // 将字节数组转换为QPixmap
    QPixmap avatar;
    bool success = avatar.loadFromData(responseData, "PNG"); // 根据服务器返回的格式指定类型

    if (success)
    {
        // 将头像设置到UI中
        newPage4->avatarLabel->setPixmap(
            avatar.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        qDebug() << "User avatar set successfully.";
    }
    else
    {
        qDebug() << "Failed to load avatar from response data.";
    }
}







